/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormularioAcompanante.java
 *
 * Created on 23/06/2010, 09:29:29 PM
 */

package CapaGrafica;
import CapaDatos.Fecha;
import CapaLogica.ManejoAcompanantes;
import CapaLogica.ValidarFormulario;
import java.awt.Color;
import javax.swing.JOptionPane;
/**
 *
 * @author Maria
 */
public class FormularioAcompanante extends javax.swing.JFrame {
    private AgregarAcompanante ventanaMadre;
    private ManejoAcompanantes acompanantes;

    /** Creates new form FormularioAcompanante */
    public FormularioAcompanante(AgregarAcompanante ventanaMadre, ManejoAcompanantes acompanantes) {
        this.ventanaMadre = ventanaMadre;
        this.acompanantes = acompanantes;
        initComponents();
        llenarComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBagregar = new javax.swing.JButton();
        jBcancelar = new javax.swing.JButton();
        jTFnombre = new javax.swing.JTextField();
        jTFapellido = new javax.swing.JTextField();
        jTFcedula = new javax.swing.JTextField();
        jLnombre = new javax.swing.JLabel();
        jLapellido = new javax.swing.JLabel();
        jLcedula = new javax.swing.JLabel();
        jCBsexo = new javax.swing.JComboBox();
        jLsexo = new javax.swing.JLabel();
        jCBdia = new javax.swing.JComboBox();
        jCBmes = new javax.swing.JComboBox();
        jCBano = new javax.swing.JComboBox();
        jLfechaNacimiento = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setLocationByPlatform(true);

        jBagregar.setText("Agregar");
        jBagregar.setName("jBagregar"); // NOI18N
        jBagregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBagregarActionPerformed(evt);
            }
        });

        jBcancelar.setText("Cancelar");
        jBcancelar.setName("jBcancelar"); // NOI18N
        jBcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcancelarActionPerformed(evt);
            }
        });

        jTFnombre.setName("jTFnombre"); // NOI18N

        jTFapellido.setName("jTFapellido"); // NOI18N

        jTFcedula.setName("jTFcedula"); // NOI18N

        jLnombre.setText("Nombre");
        jLnombre.setName("jLnombre"); // NOI18N

        jLapellido.setText("Apellido");
        jLapellido.setName("jLapellido"); // NOI18N

        jLcedula.setText("Cedula");
        jLcedula.setName("jLcedula"); // NOI18N

        jCBsexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<seleccione>", "M", "F" }));
        jCBsexo.setName("jCBsexo"); // NOI18N

        jLsexo.setText("Sexo:");
        jLsexo.setName("jLsexo"); // NOI18N

        jCBdia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dia" }));
        jCBdia.setName("jCBdia"); // NOI18N

        jCBmes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mes" }));
        jCBmes.setName("jCBmes"); // NOI18N

        jCBano.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ano" }));
        jCBano.setName("jCBano"); // NOI18N

        jLfechaNacimiento.setText("Fecha nacimiento");
        jLfechaNacimiento.setName("jLfechaNacimiento"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLnombre)
                            .addComponent(jLapellido)
                            .addComponent(jLcedula)
                            .addComponent(jLsexo)
                            .addComponent(jLfechaNacimiento))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTFcedula, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTFapellido, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTFnombre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                            .addComponent(jCBsexo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jCBdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jCBmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCBano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(123, 123, 123))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBagregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 158, Short.MAX_VALUE)
                        .addComponent(jBcancelar)
                        .addGap(85, 85, 85))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLnombre)
                    .addComponent(jTFnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLapellido)
                    .addComponent(jTFapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLcedula)
                    .addComponent(jTFcedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCBsexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLsexo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLfechaNacimiento)
                    .addComponent(jCBdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBagregar)
                    .addComponent(jBcancelar))
                .addGap(53, 53, 53))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBagregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBagregarActionPerformed
       String confirmacion = "Se ha agregado un nuevo acompañante a su reservación";
        String camposInvalidos =
                "<html>Debe llenar todos los campos para poder agregar un acompañante<br />" +
                "En caso de ser menor de edad, no es necesario el número de cédula";

        String nombre = jTFnombre.getText();
        String apellido = jTFapellido.getText();
        String cedula = jTFcedula.getText();
        char sexo = ((String) jCBsexo.getSelectedItem()).charAt(0);
        String fechaNacimiento = null;

        // Se valida que todos los campos hayan sido llenados
        boolean camposValidos = !(nombre.isEmpty() || apellido.isEmpty() || sexo == '<');

        /**
         * Se valida que se haya seleccionado una fecha y que si el acompañante
         * es mayor de edad se haya ingresado la cédula de identidad
         */
        if (camposValidos && fechaValida())
        {
            fechaNacimiento =
                    (String) jCBdia.getSelectedItem() + "/" +
                    (String) jCBmes.getSelectedItem() + "/" +
                    (String) jCBano.getSelectedItem();

            if (!ValidarFormulario.esMayorDeEdad(fechaNacimiento))
                cedula = "0";
            else if (!cedula.isEmpty())
            {
                camposValidos = ValidarFormulario.stringNumerico(cedula);
            }
        }
        else
        {
            camposValidos = false;
        }

        /**
         * Si todos los campos son válidos se precede a intentar agregar el acompañante
         * y se procede a mostrar al usuario el resultado de la operación.
         * Si algún campo es inválido se le notifica al usuario para que lo corrija.
         */
        if (camposValidos)
        {
            acompanantes.agregar(nombre, apellido, cedula, sexo, fechaNacimiento);
            JOptionPane.showMessageDialog (this, confirmacion);
            ControlDeAcompanantes ventana = new ControlDeAcompanantes(ventanaMadre, acompanantes);
            this.setVisible(false);
            ventana.setVisible(true);
        }
        else
            JOptionPane.showMessageDialog (this, camposInvalidos); // TODO add your handling code here:
    }//GEN-LAST:event_jBagregarActionPerformed

    private void jBcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcancelarActionPerformed
        ventanaMadre.setVisible(true);
        this.dispose();// TODO add your handling code here:
    }//GEN-LAST:event_jBcancelarActionPerformed

    /**
    * @param args the command line arguments
    */
   private void actualizarDias() {
        int mes, año, i, l, maxDia = 31;
        
        mes = Integer.parseInt((String) jCBmes.getSelectedItem());
        l = jCBdia.getItemCount() - 1;
        
        if (mes == 2)
        {
            maxDia = 29;
            if (!jCBano.getSelectedItem().equals("Año"))
            {
                año = Integer.parseInt((String) jCBano.getSelectedItem());
                if (año % 400 != 0 && ((año % 4 != 0) || (año % 100 == 0)))
                    maxDia--;
            }
        }
        else if ((mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))
            maxDia = 30;
        
        if (maxDia < l)
            for (i = l; i > maxDia; i--)
                jCBdia.removeItemAt(i);

        if (maxDia > l)
            for (i = l; i < maxDia; i++)
                jCBdia.addItem (String.valueOf(i + 1));
    }
   private void llenarComboBox() {
        Integer i;
        Fecha fechaActual = new Fecha();
        int año = fechaActual.get(Fecha.YEAR);

        for (i = 1; i <= 31; i++)
            jCBdia.addItem(i.toString());

        for (i = 1; i <= 12; i++)
            jCBmes.addItem(i.toString());

        for (i = año - 100; i < año; i++)
            jCBano.addItem(i.toString());
    }
   private boolean fechaValida () {
        return
            (
                (jCBdia.getSelectedIndex() != 0) &&
                (jCBmes.getSelectedIndex() != 0) &&
                (jCBano.getSelectedIndex() != 0)
            );
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBagregar;
    private javax.swing.JButton jBcancelar;
    private javax.swing.JComboBox jCBano;
    private javax.swing.JComboBox jCBdia;
    private javax.swing.JComboBox jCBmes;
    private javax.swing.JComboBox jCBsexo;
    private javax.swing.JLabel jLapellido;
    private javax.swing.JLabel jLcedula;
    private javax.swing.JLabel jLfechaNacimiento;
    private javax.swing.JLabel jLnombre;
    private javax.swing.JLabel jLsexo;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTFapellido;
    private javax.swing.JTextField jTFcedula;
    private javax.swing.JTextField jTFnombre;
    // End of variables declaration//GEN-END:variables

}
